from influxdb import DataFrameClient
import argparse


class influxdbHelper:
    def __init__(self):
        self.host = '192.168.3.90'
        self.client = DataFrameClient(self.host, 8086, '')

    def createtest():

        client = DataFrameClient(self.host, 8086, '')  # 连接数据库
        client.create_database('example')  # 创建数据库

        points = [  # 待写入数据库的点组成的列表
            {
                "measurement": "cpu_load_short",
                "tags": {
                    "host": "server01",
                    "region": "us-west"
                },
                "time": "2009-11-10T23:00:00Z",
                "fields": {
                    "value": 0.64
                }
            }
        ]
        client.write_points(points, database='example')  # 将这些点写入指定database
        # 查询刚刚写入的点
        result = client.query('select value from cpu_load_short;',
                              database='example')
        print(result)

    '''
    
    client = DataFrameClient(host, port, user, password, dbname)

    print("创建 pandas DataFrame")
    df = pd.DataFrame(data=list(range(30)),
                      index=pd.date_range(start='2014-11-16',
                                          periods=30, freq='H'), columns=['0'])

    print("创建数据库: " + dbname)
    client.create_database(dbname)

    print("将DataFrame写入数据库")
    client.write_points(df, 'demo', protocol=protocol)

    print("写带标签的DataFrame")
    client.write_points(df, 'demo',
                        {'k1': 'v1', 'k2': 'v2'}, protocol=protocol)

    print("查询")
    client.query("select * from demo")

    print("删除数据库: " + dbname)
    client.drop_database(dbname)

'''

    def parse_args():
        """解析args"""
        parser = argparse.ArgumentParser(
            description='example code to play with InfluxDB')
        parser.add_argument('--host',
                            type=str,
                            required=False,
                            default='localhost',
                            help='hostname of InfluxDB http API')
        parser.add_argument('--port',
                            type=int,
                            required=False,
                            default=8086,
                            help='port of InfluxDB http API')
        return parser.parse_args()

    def get_kline(self):
        result = self.client.query(
            "select * from KlineDay where symbol = 'HSI'",
            database='KlineIndex').get_points()

        return result
